import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class DropPanel extends JPanel{

        JPanel basePanel = new JPanel();
        JPanel panel = new JPanel();
        JButton drop=new JButton();
        JButton home=new JButton();
        JPanel panelt = new JPanel();
        JPanel panelc = new JPanel();
        JPanel panelb = new JPanel();
        String[] cols={"Drop","Term", "Course Title","Course Number", "Professor", "Meeting Info", "Credits"};
        boolean j=false;
        Object data[][]={};
        JTable table;
        DefaultTableModel dtm;
        int[] list; 
        int row;
        Image img;
        
public DropPanel() throws SQLException{
        setLayout(new BorderLayout());
        add(panel,BorderLayout.PAGE_START);
    	img=new ImageIcon("drop_background.png").getImage();
                panel.setLayout(new BorderLayout());
                panel.setBackground(Color.WHITE);
                panel.setPreferredSize(new Dimension(600,400));
                panel.setOpaque(false);
                setOpaque(false);
                
                //----TOP PANEL-----
                panelt.setBorder(BorderFactory.createTitledBorder("Drop Section"));
                panelt.setBackground(Color.WHITE);
        panelt.setLayout(new BorderLayout());
        JLabel ds=new JLabel(MenuPanel.fname+" "+MenuPanel.lname);
        JLabel name=new JLabel("1234567");
        JPanel empty=new JPanel();
        empty.setPreferredSize(new Dimension(600,20));
        empty.setOpaque(false);
        JPanel p=new JPanel();
        p.setLayout(new BorderLayout());
        p.setOpaque(false);
        //panelt.add(empty,BorderLayout.NORTH);
        panelt.add(ds,BorderLayout.CENTER);
        panelt.add(name,BorderLayout.SOUTH);
        p.add(empty,BorderLayout.NORTH);
        p.add(panelt,BorderLayout.CENTER);
        panel.add(p, BorderLayout.NORTH);
                
                
        //-----CENTER PANEL-----
        panelc.setBackground(Color.WHITE);
        panelc.setLayout(new BorderLayout());
        panelc.setBorder(BorderFactory.createTitledBorder("Select a class to drop:"));
        panelc.setPreferredSize(new Dimension(600,300));
        
        
       initialize();
       dtm.isCellEditable(0, 0);
        table.setRowSelectionAllowed(true);
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
                
        TableColumn column;
        column = table.getColumnModel().getColumn(0);
        column.setPreferredWidth(25);
                
        column = table.getColumnModel().getColumn(1);
        column.setPreferredWidth(45);
        
        column = table.getColumnModel().getColumn(2);
        column.setPreferredWidth(135);
        
        column = table.getColumnModel().getColumn(3);
        column.setPreferredWidth(45);
                
        column = table.getColumnModel().getColumn(4);
        column.setPreferredWidth(50);
        
        column = table.getColumnModel().getColumn(5);
        column.setPreferredWidth(184);
        
        column = table.getColumnModel().getColumn(6);
        column.setPreferredWidth(34);
        
        JPanel empty2=new JPanel();
        empty2.setPreferredSize(new Dimension(600,20));
        empty2.setOpaque(false);
        JPanel p2=new JPanel();
        p2.setOpaque(false);
        p2.setLayout(new BorderLayout());
        
        panelc.add(table.getTableHeader(), BorderLayout.PAGE_START);
        panelc.add(table, BorderLayout.CENTER);
        
        p2.add(empty2,BorderLayout.NORTH);
        p2.add(panelc,BorderLayout.CENTER);
        panel.add(p2,BorderLayout.WEST);
        
                
                
                //-----BASE PANEL-----
                panelb.setBackground(Color.WHITE);
                panelb.setLayout(new BorderLayout());
                panelb.add(drop, BorderLayout.WEST);
                panelb.add(home, BorderLayout.EAST);
                panelb.setOpaque(false);
                panel.add(panelb, BorderLayout.SOUTH);
                add(panel, BorderLayout.PAGE_START);
                
                final ImageIcon ho=new ImageIcon("home_button.png");
        		final ImageIcon cho=new ImageIcon("home_cbutton.png");
        		final ImageIcon de=new ImageIcon("delete_button.png");
        		final ImageIcon cde=new ImageIcon("delete_cbutton.png");
        		button(home,ho);
        		button(drop,de);
        		
                
                home.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                
                remove(panel);
                add(new MenuPanel(), BorderLayout.PAGE_START);
                validate();
                repaint();
                
            }
    });
                home.setContentAreaFilled(false);
                home.setIcon(ho);
                home.setRolloverIcon(cho);
                home.setPressedIcon(cho);
                home.getModel().addChangeListener(new ChangeListener() {
        		        @Override
        		        public void stateChanged(ChangeEvent e) {
        		            ButtonModel model = (ButtonModel) e.getSource();
        		            if (model.isRollover()) {
        		            	home.setIcon(cho);
        		            } else {
        		            	home.setIcon(ho);
        		            }// etc ...//  if (model.isPressed()) {
        		         }
        		    });
                
                drop.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                 
                int c = row;//data.length;
                for(int i =0; i<dtm.getRowCount(); i++){
                        
                        Boolean b = (Boolean) table.getModel().getValueAt(i, 0);
                        String s=(String) table.getModel().getValueAt(i, 3);
                        //System.out.println(b);
                        if(b==true)
                                {
                        				dtm.removeRow(i);
                                        System.out.println(s);
                                        update(s);
                                        i--;
                                        
                                }
                        
                }
                validate();
                repaint();
            }
    });
                drop.setContentAreaFilled(false);
                drop.setIcon(de);
                drop.setRolloverIcon(cde);
                drop.setPressedIcon(cde);
                drop.getModel().addChangeListener(new ChangeListener() {
        		        @Override
        		        public void stateChanged(ChangeEvent e) {
        		            ButtonModel model = (ButtonModel) e.getSource();
        		            if (model.isRollover()) {
        		            	drop.setIcon(cde);
        		            } else {
        		            	drop.setIcon(de);
        		            }// etc ...//  if (model.isPressed()) {
        		         }
        		    });
                
        }

	private void initialize(){
	
	dtm=new DefaultTableModel(data,cols){
		public Class getColumnClass(int c) {
            switch (c) {
              case 0: return Boolean.class;
              default: return String.class;
            }   
        }
		 @Override
       public boolean isCellEditable(int row, int col) {
            switch (col) {
                case 0:
                    return true;
                default:
                    return false;
             }
       }
       
	};
	table=new JTable(dtm);
	
	try{
		String dName = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
		Connection con = DriverManager.getConnection(url);
		java.sql.Statement mysqlStatement = con.createStatement();
		java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT cu.term, cu.start_time, cu.end_time, cu.days, cu.instructor, ca.course_name, ca.course_number, ca.credit_hours, cu.CID FROM next_semester_chosen_course_list AS cu JOIN course_catalog AS ca ON cu.CID = ca.CID WHERE cu.CID IN (SELECT CID FROM next_semester_chosen_course_list WHERE SID='"+MenuPanel.SID+"') GROUP BY course_name");
		
		while (resultSet.next()) {
			row+=1;
			String term=resultSet.getString(1);
			String start_time="";//=resultSet.getString(2);
			String end_time="";//=resultSet.getString(3);
			//String days;//=resultSet.getString(4);
			String instructor=resultSet.getString(5);
			String course_name=resultSet.getString(6);
			String course_number=resultSet.getString(7);
			
			int credit_hours=resultSet.getInt(8);
			String cid=resultSet.getString(9);
			String days="";
			
			try{
				String dName2 = "com.mysql.jdbc.Driver";
				String url2 = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
				Connection con2 = DriverManager.getConnection(url2);
				java.sql.Statement mysqlStatement2 = con2.createStatement();
				//java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT course_name, start_time, days From course_catalog, current_course_total_list WHERE course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
				java.sql.ResultSet resultSet2 = mysqlStatement2.executeQuery("SELECT co.start_time, co.end_time, co.days, co.CID From course_catalog AS ca JOIN next_semester_chosen_course_list AS co ON ca.CID = co.CID WHERE ca.CID ='"+cid+"' AND co.SID='"+MenuPanel.SID+"'");//start_time='"+start_time+"'");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
				
				//while (resultSet.next()&&resultSet2.next()) {
					while(resultSet2.next()){
					//String course_number= resultSet.getString(1);
					//String course_section=resultSet.getString(3);
					//course=course_number+"-"+course_name+"-"+course_section;
					
					String startTime=resultSet2.getString(1);
					String endTime=resultSet2.getString(2);
					String day=resultSet2.getString(3);
					start_time=startTime;
					end_time=endTime;
					
					//start_time+=startTime;
					//end_time+=endTime;
					days+=day;
					
					System.out.println(cid+": "+startTime+ " "+endTime+" "+days+" ");
					
					//j=new boolean[row][7];
				}
				con2.close();
				}
				catch(Exception ex){}
			String dd=days+" "+start_time+":00 - "+end_time+":00";
			
			Object[] program={j,term,course_name,course_number,instructor,dd,credit_hours};
			dtm.addRow(program);
			System.out.println("added");
		}
		con.close();
		}
		catch(Exception ex){}
}

	private void update(String cname){
		try{
			String dName = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
			Connection con = DriverManager.getConnection(url);
			java.sql.Statement mysqlStatement = con.createStatement();
			mysqlStatement.executeUpdate("DELETE FROM next_semester_chosen_course_list WHERE CID IN (SELECT CID FROM course_catalog WHERE course_number='"+cname+"')");
			System.out.println(cname+" "+"dropped?");
			con.close();
			}
			catch(Exception ex){}
	}
	
	@Override
    public void paintComponent(Graphics g) {
		super.paintComponents(g);
        g.drawImage(img, 0, 0, this);
    }
	
	private void button(JButton b, ImageIcon i){
		b.setBorderPainted(false);
		b.setBorder(null);
		//button.setFocusable(false);
		b.setMargin(new Insets(0, 0, 0, 0));
		b.setContentAreaFilled(false);
		b.setIcon(i);
		//b.setRolloverIcon(myIcon2);
		//b.setPressedIcon(myIcon3);
	}
}